﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLThuVien
{
    public partial class frTraSach : Form
    {
        public frTraSach()
        {
            InitializeComponent();
            btktrasach.Enabled = false;
            btgiahan.Enabled = false;
            bttrasach.Enabled = false;
        }
        public void loadview()
        {
            listView1.Clear();
            listView1.Columns.Add("stt", 60, HorizontalAlignment.Center);
            listView1.Columns.Add("Mã Sách", 120, HorizontalAlignment.Center);
            listView1.Columns.Add("Tên Sách", 180, HorizontalAlignment.Center);
            listView1.Columns.Add("Ngày Mượn", 150, HorizontalAlignment.Center);
            KetNoi.setconnect();
            DataSet ds = new DataSet();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT SACH.MASACH,SACH.TENSACH,PHIEUMUON.NGAYMUON FROM DOCGIA,PHIEUMUON,SACH_PHIEU,SACH WHERE DOCGIA.MADOCGIA='" + tbmadocgia.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH = SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
            SqlDataAdapter da = new SqlDataAdapter(sc.CommandText, KetNoi.scn);
            da.Fill(ds);
            DataColumn dc = new DataColumn();
            ds.Tables[0].Columns.Add(dc);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                listView1.Items.Add((i + 1).ToString(), 1);
                listView1.Items[i].SubItems.Add(ds.Tables[0].Rows[i]["MASACH"].ToString());
                listView1.Items[i].SubItems.Add(ds.Tables[0].Rows[i]["TENSACH"].ToString());
                listView1.Items[i].SubItems.Add(ds.Tables[0].Rows[i]["NGAYMUON"].ToString());
            }
        }
        int sosachmuon;

        private void btktdocgia_Click(object sender, EventArgs e)
        {
            int i = 0;
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT *FROM DOCGIA,PHIEUMUON,SACH_PHIEU,SACH WHERE DOCGIA.MADOCGIA='" + tbmadocgia.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH = SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                tbTendocgia.Text = Convert.ToString(dr["HOTEN"]);
                i++;
            }
            tbsosachmuon.Text = Convert.ToString(i);
            sosachmuon = i;
            loadview();
            tbmasach.Enabled = true;
            tbmadocgia.Enabled = false;
            btktrasach.Enabled = true;
            btktdocgia.Enabled = false;    
        }
        string ngaymuon;
        int monthm, daym, yearm;
        int songayquahan;
        private void btktrasach_Click(object sender, EventArgs e)
        {
            int month = Convert.ToInt16(DateTime.Now.Month);
            int day = Convert.ToInt16(DateTime.Now.Day);
            int year = Convert.ToInt16(DateTime.Now.Year);
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT *FROM SACH_PHIEU,SACH WHERE SACH.MASACH='" + tbmasach.Text + "' AND SACH_PHIEU.MASACH = SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                ngaymuon = Convert.ToString(dr["NGAYHETHANTRA"]);
            }
            if (ngaymuon.Substring(1, 1) == "/")
            {
                monthm = int.Parse(ngaymuon.Substring(0, 1));
                if (ngaymuon.Substring(3, 1) == "/")
                {
                    daym = int.Parse(ngaymuon.Substring(2, 1));
                    yearm = Convert.ToInt16(ngaymuon.Substring(4, 4));
                }
                else if (ngaymuon.Substring(4, 1) == "/")
                {
                    daym = int.Parse(ngaymuon.Substring(2, 2));
                    yearm = int.Parse(ngaymuon.Substring(5, 4));
                }
            }
            else
            {
                monthm = int.Parse(ngaymuon.Substring(0, 2));
                if (ngaymuon.Substring(4, 1) == "/")
                {
                    daym = int.Parse(ngaymuon.Substring(3, 1));
                    yearm = Convert.ToInt16(ngaymuon.Substring(5, 4));
                }
                else if (ngaymuon.Substring(5, 1) == "/")
                {
                    daym = int.Parse(ngaymuon.Substring(3, 2));
                    yearm = int.Parse(ngaymuon.Substring(6, 4));
                }
            }
            songayquahan = (year - yearm) * 365 + (month - monthm) * 30 + (day - daym);
            if (songayquahan < 0) songayquahan = 0;
            tbsongayquahan.Text = Convert.ToString(songayquahan);
            bttrasach.Enabled = true;
            btgiahan.Enabled = true;
        }

        string maphieu = "";
        int tienphat, giasach;
        bool matsach = false;
        private void bttrasach_Click(object sender, EventArgs e)
        {
            string xuphat;
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT *FROM SACH,DOCGIA,PHIEUMUON,SACH_PHIEU WHERE DOCGIA.MADOCGIA='" + tbmadocgia.Text + "' AND SACH.MASACH='" + tbmasach.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                maphieu = Convert.ToString(dr["MAPHIEU"]);
                giasach = Convert.ToInt16(dr["GIA"]);
            }
            if (checkBox1.Checked)
            {
                xuphat = "MATSACH";
                matsach = true;
            }
            else if (songayquahan > 0)
            {
                xuphat = "TREHAN";
            }
            else xuphat = "";
            Truyxuatdulieu txdl1 = new Truyxuatdulieu();
            SqlCommand sc1 = new SqlCommand();
            sc1.CommandText = "SELECT THAMSOXUPHAT.MUCPHAT FROM THAMSOXUPHAT WHERE THAMSOXUPHAT.TENXUPHAT='" + xuphat + "'";
            SqlDataReader dr1 = txdl1.lay_reader(sc1.CommandText);
            while (dr1.Read())
            {
                tienphat = Convert.ToInt16(dr1["MUCPHAT"]);
            }
            int mucphat = tienphat;
            if (xuphat == "MATSACH")
            {
                tienphat = tienphat * giasach;
            }
            else tienphat = tienphat * songayquahan;
            string today = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            PhieuSach ps = new PhieuSach();
            ps.set_maphieumuon(maphieu);
            ps.set_matsach(matsach);
            ps.set_ngaytra(Convert.ToDateTime(today));
            ps.set_songayquahan(songayquahan);
            ps.set_tienphat(tienphat);
            ps.set_masach(tbmasach.Text);
            if (ps.capnhatphieusach())
            {
                if (matsach == true)
                {
                    MessageBox.Show("Độc giả phải đền gấp " + mucphat + " lần giá trị quyển sách", "Thông Báo");
                    sosachmuon--;
                    tbxuphat.Text = Convert.ToString(ps.get_tienphat());
                }
                else if (songayquahan > 0)
                {
                    MessageBox.Show("Độc giả đã trả sách trễ hạn " + songayquahan + " ngày", "Thông Báo");
                    sosachmuon--;
                    tbxuphat.Text = Convert.ToString(ps.get_tienphat());
                }
                else
                {
                    MessageBox.Show("Đã trả sách thành công", "Thông Báo");
                    sosachmuon--;
                    tbsosachmuon.Text = Convert.ToString(sosachmuon);
                }
            }
            else
                MessageBox.Show("Có lỗi ko update được phiếu mượn", "Thông Báo");
            loadview();
        }

        string mapmgiahan = "";
        string ngayhethangh = "";
        private void btgiahan_Click(object sender, EventArgs e)
        {
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlCommand sc = new SqlCommand();
            sc.CommandText = "SELECT PHIEUMUON.MAPHIEU FROM PHIEUMUON,SACH_PHIEU,DOCGIA,SACH WHERE DOCGIA.MADOCGIA='" + tbmadocgia.Text + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH = SACH.MASACH";
            SqlDataReader dr = txdl.lay_reader(sc.CommandText);
            while (dr.Read())
            {
                mapmgiahan = Convert.ToString(dr["MAPHIEU"]);
            }
            string today = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            int day = Convert.ToInt16(DateTime.Now.Day.ToString());
            int month = Convert.ToInt16(DateTime.Now.Month.ToString());
            int year = Convert.ToInt16(DateTime.Now.Year.ToString());
            if (month + 3 > 12)
            {
                month = month + 3 - 12;
                year += 1;
            }
            else month += 3;
            ngayhethangh = Convert.ToString(month) + "/" + Convert.ToString(day) + "/" + Convert.ToString(year);
            PhieuSach PS = new PhieuSach();
            PS.set_maphieumuon(mapmgiahan);
            PS.set_masach(tbmasach.Text);
            PS.set_ngayhethantra(Convert.ToDateTime(ngayhethangh));
            if (PS.giahan())
            {
                MessageBox.Show("Đã gia hạn thành công", "Thông Báo");
                loadview();
            }
            else MessageBox.Show("có lỗi ko gia hạn được");
        }

        private void bthuytt_Click(object sender, EventArgs e)
        {
            tbmasach.Text = "";
            tbmadocgia.Text = "";
            tbsongayquahan.Text = "";
            tbsosachmuon.Text = "";
            tbTendocgia.Text = "";
            tbxuphat.Text = "";
            checkBox1.Checked = false;
            btktdocgia.Enabled = true;
            btktrasach.Enabled = false;
            tbmasach.Enabled = false;
            btgiahan.Enabled = false;
            bttrasach.Enabled = false;
            tbmadocgia.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

    }
}
